#include "imageitem.h"
QSize ImageItem::BG_SIZE = QSize(800, 554);

ImageItem::ImageItem(const QString &imgName,
                     const QPoint &leftTop,
                     const QPoint &rightBottom,
                     QGraphicsScene *scene,
                     QGraphicsItem *parent) :
    QObject(), QGraphicsItem(parent, scene), imageName(imgName)
{
    qreal xfac = qreal(scene->width())/BG_SIZE.width();
    qreal yfac = qreal(scene->height())/BG_SIZE.height();

    imageSize =  QSize((rightBottom.x()-leftTop.x())*xfac,
                       (rightBottom.y()-leftTop.y())*yfac);
    if (imageSize.width()<=0 || imageSize.height()<=0 || imgName.isEmpty())
        image = QPixmap(imgName);
    else
        image = QPixmap(imgName).scaled(imageSize);

    if (parent == NULL)
        // for item without no parent.
        this->setPos(QPointF((leftTop.x()-BG_SIZE.width()/2)*xfac,
                         (leftTop.y()-BG_SIZE.height()/2)*yfac));
    else
        this->setPos(QPointF(leftTop.x()*xfac,
                         leftTop.y()*yfac));
}

QRectF ImageItem::boundingRect() const
{
    return QRectF(QPointF(), imageSize);
}

void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    if (!image.isNull())
        painter->drawPixmap(QPointF(), image);
//    QList<QGraphicsItem *> items = this->childItems();
//    for (int i=0; i<items.size(); i++)
//        items.at(i)->paint(painter, option, widget);
}

ImageItem::~ImageItem()
{

}

